package com.goods.business.service.imp;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.goods.business.converter.InStockConverter;
import com.goods.business.converter.ProductConverter;
import com.goods.business.mapper.*;
import com.goods.business.service.InStockService;
import com.goods.business.service.SupplierService;
import com.goods.common.model.business.*;
import com.goods.common.utils.JWTUtils;
import com.goods.common.utils.ListPageUtils;
import com.goods.common.vo.business.InStockItemVO;
import com.goods.common.vo.business.InStockVO;
import com.goods.common.vo.business.ProductVO;
import com.goods.common.vo.business.SupplierVO;
import com.goods.common.vo.system.PageVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.*;
import java.util.stream.Collectors;

@Service
public class InStockServiceImpl implements InStockService {
    @Resource
    private InStockMapper inStockMapper;
    @Autowired
    private SupplierService supplierService;
    @Resource
    private SupplierMapper supplierMapper;
    @Resource
    private ProductMapper productMapper;
    @Resource
    private InStockInfoMapper inStockInfoMapper;
    @Resource
    private ProductStockMapper productStockMapper;

    @Override
    public PageVO<InStockVO> findInStockList(Map<String, Object> queryMap) {
        Example example = new Example(InStock.class);
        if (queryMap.get("type") != null && !"".equals(queryMap.get("type"))){
            example.createCriteria().andEqualTo("type",queryMap.get("type"));
        }
        if (queryMap.get("inNum") != null && !"".equals(queryMap.get("inNum"))){
            example.createCriteria().andEqualTo("inNum",queryMap.get("inNum"));
        }
        if (queryMap.get("status") != null && !"".equals(queryMap.get("status"))){
            example.createCriteria().andEqualTo("status",queryMap.get("status"));
        }
        if (queryMap.get("startTime") != null && !"".equals(queryMap.get("startTime"))){
            example.createCriteria().andEqualTo("createTime",queryMap.get("startTime"));
        }
        if (queryMap.get("endTime") != null && !"".equals(queryMap.get("endTime"))){
            example.createCriteria().andEqualTo("modified",queryMap.get("endTime"));
        }
        List<InStock> stocks = inStockMapper.selectByExample(example);
        List<InStockVO> inStockVOList = InStockConverter.converterToInStockVOList(stocks);
        if (!CollectionUtils.isEmpty(inStockVOList)){
            for (InStockVO inStockVO : inStockVOList) {
                if (inStockVO.getSupplierId() != null){
                    Supplier supplier = supplierService.getBySupplierId(inStockVO.getSupplierId());
                    inStockVO.setSupplierName(supplier.getName());
                    inStockVO.setPhone(supplier.getPhone());
                }
            }
            String pageSize = (String)queryMap.get("pageSize");
            String pageNum = (String)queryMap.get("pageNum");
            List<InStock> page = ListPageUtils.page(stocks,Integer.parseInt(pageSize), Integer.parseInt(pageNum));
            return new PageVO<>(page.size(),inStockVOList);
        }
        return new PageVO<>(0L,null);
    }
    //入库记录的明细
    @Override
    public Map<Object, Object> detail(Long id, Integer pageNum) {
        //返回结果为个map
        Map<Object, Object> retMap = new HashMap<>();
        //根据入库记录id获取信息
        InStock inStock = inStockMapper.selectByPrimaryKey(id);
        Supplier supplier = supplierService.getBySupplierId(inStock.getSupplierId());
        SupplierVO supplierVO = new SupplierVO();
        BeanUtils.copyProperties(supplier,supplierVO);
        retMap.put("supplierVO",supplierVO);

        Integer status = inStock.getStatus();
        retMap.put("status",status);

        //根据入库记录的id获取 入库记录对应的相关信息 封装到InStockItemVO中
        List<InStockItemVO> inStockItemVOS = inStockMapper.selelctAboutInfoById(id);

        List<InStockItemVO> page = ListPageUtils.page(inStockItemVOS, 5, pageNum);
        retMap.put("total",page.size());
        retMap.put("itemVOS",inStockItemVOS);
        return retMap;
    }
    //放入回收站
    @Override
    public void remove(Long id) {
        InStock inStock = inStockMapper.selectByPrimaryKey(id);
        if (inStock != null){
            inStock.setStatus(1);
            inStock.setId(id);
            inStock.setModified(new Date());
            inStockMapper.updateByPrimaryKey(inStock);
        }
    }
    //恢复状态
    @Override
    public void back(Long id) {
        InStock inStock = inStockMapper.selectByPrimaryKey(id);
        if (inStock != null){
            inStock.setStatus(0);
            inStock.setId(id);
            inStock.setModified(new Date());
            inStockMapper.updateByPrimaryKey(inStock);
        }
    }
    //删除入库记录
    @Override
    public void delete(Long id) {
        InStock inStock = inStockMapper.selectByPrimaryKey(id);
        if (inStock != null){
            inStockMapper.delete(inStock);
        }
    }
    //添加入库记录
    @Override
    public void addIntoStock(InStockVO inStockVO,String username) {
        String inNum = UUID.randomUUID().toString();
        List<Object> products = inStockVO.getProducts();
        InStockInfo inStockInfo = new InStockInfo();
        for (Object object : products) {
            //将数据强转为map因为前端传的是 key value
            Map<String, Object> map = (Map<String, Object>)object;
            Object productId = map.get("productId");
            Integer productNumber = (Integer)map.get("productNumber");//productNumber
            Product product = productMapper.selectByPrimaryKey(productId);
            String pNum = product.getPNum();

            //入库详情信息
            inStockInfo.setInNum(inNum);
            inStockInfo.setPNum(pNum);
            inStockInfo.setCreateTime(new Date());
            inStockInfo.setModifiedTime(new Date());
            inStockInfo.setProductNumber(productNumber);
            inStockInfoMapper.insert(inStockInfo);
        }

        if (inStockVO.getSupplierId() == null){
            Supplier supplier = new Supplier();
            //物资来源信息
            supplier.setAddress(inStockVO.getAddress());
            supplier.setName(inStockVO.getName());
            supplier.setContact(inStockVO.getContact());
            supplier.setEmail(inStockVO.getEmail());
            supplier.setPhone(inStockVO.getPhone());
            supplier.setSort(inStockVO.getSort());
            supplier.setCreateTime(new Date());
            supplier.setModifiedTime(new Date());
            supplierMapper.insert(supplier);

            //入库记录信息
            InStock inStock = new InStock();
            inStock.setStatus(2);
            inStock.setInNum(inNum);
            inStock.setType(inStockVO.getType());
            inStock.setOperator(username);
            inStock.setCreateTime(new Date());
            inStock.setModified(new Date());
            inStock.setProductNumber(products.size());
            inStock.setSupplierId(supplier.getId());
            inStock.setRemark(inStockVO.getRemark());
            inStockMapper.insert(inStock);
        }else {
            //入库记录信息
            InStock inStock = new InStock();
            inStock.setStatus(2);
            inStock.setInNum(inNum);
            inStock.setType(inStockVO.getType());
            inStock.setOperator(username);
            inStock.setCreateTime(new Date());
            inStock.setModified(new Date());
            inStock.setProductNumber(products.size());
            inStock.setSupplierId(inStockVO.getSupplierId());
            inStock.setRemark(inStockVO.getRemark());
            inStockMapper.insert(inStock);
        }

    }
    //入库记录的审核
    @Override
    public void publish(Long id) {
        InStock inStock = inStockMapper.selectByPrimaryKey(id);
        inStock.setStatus(0);
        inStockMapper.updateByPrimaryKey(inStock);
        //增加库存
        String inNum = inStock.getInNum();
        Example example = new Example(InStockInfo.class);
        example.createCriteria().andEqualTo("inNum",inNum);
        List<InStockInfo> inStockInfoList = inStockInfoMapper.selectByExample(example);
        if (!CollectionUtils.isEmpty(inStockInfoList)){
            for (InStockInfo inStockInfo : inStockInfoList) {
                String pNum = inStockInfo.getPNum();
                Integer productNumber = inStockInfo.getProductNumber();
                Example example1 = new Example(ProductStock.class);
                example1.createCriteria().andEqualTo("pNum",pNum);
                List<ProductStock> productStocks = productStockMapper.selectByExample(example1);
                for (ProductStock productStock : productStocks) {
                    productStock.setStock(productStock.getStock()+productNumber);
                    productStockMapper.updateByPrimaryKey(productStock);
                }
            }
        }
    }
}
